<?php


namespace App\Api\V1\Controllers;


use App\Models\Ad;
use App\Models\News;
use Carbon\Carbon;
use QL\QueryList;

class IndexController extends BaseController
{

    public function get_index_data()
    {
        //获取幻灯片
        $hdp = Ad::where('ad_position_name', '=', 'index_hdp')->orderBy('order', 'desc')->limit(5)->get(['name', 'href', 'pic', 'pic_style']);
        $data['hdp'] = $hdp;
        //获取导航
        $grid_nav = Ad::where('ad_position_name', '=', 'index_grid_nav')->orderBy('order', 'desc')->limit(5)->get(['name', 'href', 'pic', 'pic_style']);
        $data['grid_nav'] = $grid_nav;
        //获取列表数据
        $news = News::where('status','=','1')->where('id', '>', '0')->orderBy('created_at','desc')->paginate(10,['id', 'title', 'describe', 'cover', 'view_num', 'zan_num', 'media_id','created_at']);
        foreach ($news as $k => $v) {
            $news[$k]->media = $v->media()->first(['id', 'name', 'avatar']);
            $news[$k]->comments_count = $v->comments()->count();
            $news[$k]->time = Carbon::parse($v->created_at)->diffForHumans();
        }
        $data['news'] = $news;
        return $this->response->array(['status_code' => 200, 'message' => '请求成功', 'data' => $data]);
    }


    public function tt()
    {
        header('Content-type: text/html; charset=utf-8');

        $w_url = 'http://mp.weixin.qq.com/s/naTVjNR3cvewNOYoE2vbSg';

        $client = new \GuzzleHttp\Client();

        $res = $client->request('get',$w_url);

        $html = $res->getBody();


        $html = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $html);

        $data = QueryList::Query($html, [
            'title' => ['title', 'html'],
            'content' => ['#js_content', 'text', 'p img iframe'],
        ])->data;

        dd($data);
    }
}